package com.jiuzhou.guanwang.jzcp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.URLConfig;
import com.jiuzhou.guanwang.jzcp.activity.LastOpenActivity;
import com.jiuzhou.guanwang.jzcp.activity.PastOpenActivity;
import com.jiuzhou.guanwang.jzcp.adapter.AwardItemAdapter;
import com.jiuzhou.guanwang.jzcp.base.DefaultAdapter;
import com.jiuzhou.guanwang.jzcp.bean.HomeCPBean;
import com.jiuzhou.guanwang.jzcp.netutil.NetRequestUtil;
import com.jiuzhou.guanwang.jzcp.netutil.response.CaiPiaoListResponse;
import com.jiuzhou.guanwang.jzcp.widget.RecycleViewDivider;
import com.vise.log.ViseLog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AwardFragment extends Fragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private View rootView;
    private Unbinder unbinder;
    private String[] support_lotterys = {"ssq", "dlt", "fc3d", "pl3", "pl5", "qxc", "qlc", "js11x5", "jsk3"};
    private List<HomeCPBean> lottery = new ArrayList<>();
    private AwardItemAdapter adapter;

    public static AwardFragment newInstance(String info) {
        Bundle args = new Bundle();
        AwardFragment fragment = new AwardFragment();
        args.putString("info", info);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            String name = bundle.get("info").toString();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_award, null);
            unbinder = ButterKnife.bind(this, rootView);
            initView();
            initData();
        } else {
            // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，
            // 要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }

        return rootView;
    }

    private void initView() {
        adapter = new AwardItemAdapter(getActivity(), lottery);
        recyclerView.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.divider_list)));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new DefaultAdapter.OnItemClickListener<HomeCPBean>() {
            @Override
            public void onClick(View view, RecyclerView.ViewHolder holder, HomeCPBean item, int position) {
                Intent intent;
                if(item.getCode().equals("js11x5")||item.getCode().equals("jsk3")){
                    intent = new Intent(getActivity(), PastOpenActivity.class);
                }else {
                     intent = new Intent(getActivity(), LastOpenActivity.class);
                }
                intent.putExtra("name", item.getName());
                intent.putExtra("code", item.getCode());
                getActivity().startActivity(intent);
            }
        });
    }


    private void initData() {
        NetRequestUtil.getInstance().post(URLConfig.CAIPIAO_LIST, null, 100, new NetRequestUtil.NetResponseListener<String>() {
            @Override
            public void onSuccess(String response, int requestCode) {
                CaiPiaoListResponse caiPiaoListResponse = new Gson().fromJson(response, CaiPiaoListResponse.class);
                List<HomeCPBean> result = caiPiaoListResponse.getShowapi_res_body().getResult();
                List<String> lotterys = Arrays.asList(support_lotterys);
                for (int i = 0; i < result.size(); i++) {
                    if (lotterys.contains(result.get(i).getCode())) {
                        lottery.add(result.get(i));
                    }
                }
                ViseLog.d(lottery);
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onFailed(String response, int requestCode) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
